package cn.lxh.org.controller;


import cn.lxh.basic.utils.PageList;
import cn.lxh.org.basic.utils.AjaxResult;
import cn.lxh.org.domain.Employee;

import cn.lxh.org.query.DepartmentQuery;
import cn.lxh.org.query.EmployeeQuery;
import cn.lxh.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emp")
@RestController
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询一条 数据  GET
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping
    public List<Employee> queryAll(){
        List<Employee> employees = employeeService.queryAll();
        return employees;
    }
    /**
     * 删除
     * @param id
     * @return
     */
   @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            System.out.println("开始");
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me();
        }
    }

    /**
     * 增加 修改
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作异常").setSuccess(false);
        }
    }

    /**
     * 分页功能
     * @param
     * @return
     *
     */
    @PostMapping
    public PageList queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryList(employeeQuery);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        employeeService.batchReove(ids);
        return new AjaxResult();
    }



}
